<template>
  <div class="keyboard">
    <Key12
      class="key-group"
      v-for="group in theGroupList"
      :key="group.id"
      :whiteKeyW="whiteKeyWidth"
      :whiteKeyH="whiteKeyHeight"
      :baseCode="group.keycode0"
    />
  </div>
</template>

<script>
import Key12 from "./key12.vue";

export default {
  name: "PianoKeyboard",
  components: { Key12 },

  props: {
    whiteKeyW: Number,
    whiteKeyH: Number,
    countGroup: Number,
  },

  computed: {
    whiteKeyHeight() {
      return this.normalizeNumber(this.whiteKeyH, 180);
    },

    whiteKeyWidth() {
      return this.normalizeNumber(this.whiteKeyW, 30);
    },

    theCountGroup() {
      return this.normalizeNumber(this.countGroup, 3);
    },

    theGroupList() {
      let offset = 4;

      let list = [];
      let count = this.theCountGroup;
      for (let i = 0; i < count; i++) {
        list.push({
          id: "key-group-" + i,
          keycode0: (i + offset) * 12,
        });
      }
      return list;
    },
  },

  methods: {
    normalizeNumber(n, def) {
      if (n == null) {
        if (def == null) {
          return 0;
        }
        return def;
      }
      return Number(n);
    },
  },
};
</script>

<style scoped>
.keyboard {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  align-content: center;
}

.key-group {
  flex-grow: 0;
  flex-basis: auto;
  flex-shrink: 1;
}
</style>
